package input;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import sim.Canvas;
import sim.Simulation;
/**
 * Takes the key input and have the canvas perform a the function specified.
 * @author Niel Lebeck and Wynne Lok
 *
 */
public class InputResponder {
    private static final int PANEL_DECEASE_AMOUNT = 20;
    private static final int PANEL_LOCATION_INCREASE = -10;
    private static final int PANEL_LOCATION_DECREASE = 10;
    private Canvas myCanvas;
/**
 * Makes sure that the canvas used is still the current one.
 * @param c is the canvas that is currently running.
 */
    public InputResponder(Canvas c) {
        myCanvas = c;
    }
/**
 * Takes the key event and performs the function requested.
 * @param keyCode is the key input the user typed.
 */
    public void manageSimulation (int keyCode) {
        manageModels(keyCode);
        manageRunningSimulation(keyCode);
        changeAreaSize(keyCode);
        toggleForces(keyCode);
    }
/**
 *  Takes the key event and performs the functions on models.
 * @param keyCode is the key input the user typed.
 */
    public void manageModels(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_N:
                myCanvas.loadModel();
                break;
            case KeyEvent.VK_C:
                myCanvas.setTarget(new Simulation(myCanvas));
                break;
            default:
                break;
        }
    }
/**
 * Manages the running of the simulation.
 * @param keyCode the key event the user typed.
 */
    public void manageRunningSimulation(int keyCode) {
        switch(keyCode) {
            case KeyEvent.VK_SPACE:
                if (myCanvas.isRunning()) {
                    myCanvas.stop();
                }
                else {
                    myCanvas.start();
                }
                break;
            case KeyEvent.VK_S:
                myCanvas.step((double) Canvas.FRAMES_PER_SECOND / Canvas.ONE_SECOND);
                break;
            case KeyEvent.VK_P:
                System.out.println(myCanvas.getTarget());
                break;
            default:
                break;
        }
    }
/**
 * Changes the area that the assembly is in.
 * @param keyCode the up or down key event that the user presses.
 */
    public void changeAreaSize(int keyCode) {
        switch(keyCode) {
            case KeyEvent.VK_UP:
                int newUpHeight = myCanvas.getSize().height + PANEL_DECEASE_AMOUNT;
                int newUpWidth = myCanvas.getSize().width + PANEL_DECEASE_AMOUNT;
                Dimension canvasSize = new Dimension(newUpWidth, newUpHeight);
                myCanvas.getTopLeftCorner().translate(PANEL_LOCATION_INCREASE,
                        PANEL_LOCATION_INCREASE);
                myCanvas.setLocation(myCanvas.getTopLeftCorner());
                myCanvas.setSize(canvasSize);
                break;
            case KeyEvent.VK_DOWN:
                int newDownHeight = myCanvas.getSize().height - PANEL_DECEASE_AMOUNT;
                int newDownWidth = myCanvas.getSize().width - PANEL_DECEASE_AMOUNT;
                Dimension canvasSizeB = new Dimension(newDownWidth,
                        newDownHeight);
                myCanvas.getTopLeftCorner().translate(PANEL_LOCATION_DECREASE,
                        PANEL_LOCATION_DECREASE);
                myCanvas.setLocation(myCanvas.getTopLeftCorner());
                myCanvas.setSize(canvasSizeB);
                break;
            default:
                break;
        }
    }
/**
 * Turns on or off different forces by calling the corresponding method in
 * the Simulation.
 * @param keyCode User presses g, v, or m for gravity, viscosity,
 * or center mass, or 1, 2, 3, or 4 for the corresponding wall force
 */
    public void toggleForces(int keyCode) {
        myCanvas.getTarget().toggleForce(keyCode);
    }
}
